<?php

namespace app\admin\controller\classes;

use app\common\controller\Backend;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Classes extends Backend
{

    /**
     * Classes模型对象
     * @var \app\admin\model\classes\Classes
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\classes\Classes;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("requireVerifyList", $this->model->getRequireVerifyList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $adming_where=[];
            if (!$this->auth->isSuperAdmin())
            {
                $adming_where['fa_classes_class.uid'] = get_admin_id($this->auth->id);
            }
            if(get_auth_group($this->auth->id) == 3)
            {
                $profile_id = get_profile_id($this->auth->id);
                $adming_where['fa_classes_class.profile_id'] = $profile_id;
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//            dump($this->request->get());die;
            $list = $this->model
                    ->with(['teacher'])
                    ->where($where)
                    ->where($adming_where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row)
            {
                $row->visible(['id','title','disable','status','grade','entry_year','profile_id','requireVerify','realname','mobile']);
				$row->visible(['teacher']);
				$row->getRelation('teacher')->visible(['realname','mobile']);

            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");

            Db::startTrans();
            try {
                if(empty($params['profile_id']))
                {
                    $tlist = Db::name('classes_teacher')->where(['user_id'=>$this->auth->id])->find();
                    $params['profile_id'] = $tlist['profile_id'];
                }
                $params['uid'] = get_admin_id($this->auth->id);
                Db::name('classes_class')->insert($params);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $agalist = Db::name('auth_group_access')->where('uid',$this->auth->id)->find();
        $is_show = 0;
        if($agalist['group_id'] == 1 || $agalist['group_id'] == 2)
        {
            $is_show = 1;
        }
        $this->view->assign('is_show',$is_show);
        $where = [];
        if($this->auth->id != 1)
        {
            $where = ['admin_id'=>$this->auth->id];
        }
        $tlist = Db::name('classes_teacher')->where($where)->select();
        $this->view->assign('tlist',$tlist);
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row = Db::name('classes_class')->find($ids);
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");

            Db::startTrans();
            try {

                Db::name('classes_class')->where('id',$row['id'])->update($params);

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }

}
